/**********************************************************************
 *** 名称 ：GameGUI.cpp
 *** 描述 ：实现GameGUI类中的方法。
 **********************************************************************/
#include "GameGUI.h"

GameGUI::GameGUI()
{
#ifdef USING_UTF8
    SDL_WM_SetCaption("华容道 v0.0.3 -- 龙霜制作", NULL);
#else
    SDL_WM_SetCaption(GameSystem::localToUTF8("华容道 v0.0.3 -- 龙霜制作"), NULL);
#endif

    SDL_WM_SetIcon(SDL_LoadBMP("image/HuanRongDao.bmp"), NULL);

    /* 设置主窗体 */
    this->mainWnd = SDL_SetVideoMode(GameSystem::WINDOW_WIDTH, GameSystem::WINDOW_HEIGHT, 16, SDL_DOUBLEBUF | SDL_HWSURFACE);
    if(this->mainWnd == NULL) {
        std::cout<<SDL_GetError()<<std::endl;
        exit(1);
    }

    /* 加载图像资源 */
    this->background = IMG_Load("image/background.jpg");
    this->selectedT = IMG_Load("image/selectedT.bmp");
    this->selectedB = IMG_Load("image/selectedB.bmp");
    this->closeSoundFlag = IMG_Load("image/closesound.bmp");
    this->roles = IMG_Load("image/roles.jpg");
    if((this->background == NULL) || (this->roles == NULL) || (this->selectedT== NULL)
        || (this->selectedB == NULL) || (this->closeSoundFlag == NULL)) {
        std::cout<<"Error while load image: "<<SDL_GetError()<<std::endl;
        exit(1);
    }
    this->selectedB = SDL_DisplayFormat(this->selectedB);
    this->selectedT = SDL_DisplayFormat(this->selectedT);
    this->closeSoundFlag = SDL_DisplayFormat(this->closeSoundFlag);

    this->font = TTF_OpenFont("C:\\Windows\\Fonts\\simhei.ttf", 18);
    if(!font) {
        std::cout<<"Error while opening fonts: "<<std::endl;
        exit(1);
    }
}

void GameGUI::initLevel(int level, std::string levelName)
{
    SDL_Color color = {255, 255, 255};
    char *strLevel = new char[20];
    memset(strLevel, 0, 20);
    sprintf(strLevel, "%i/10 %s", level, levelName.c_str());
#ifdef USING_UTF8
    this->levelTile = TTF_RenderUTF8_Blended(font, strLevel, color);
#else
    this->levelTile = TTF_RenderUTF8_Blended(font, GameSystem::localToUTF8(levelName), color);
#endif
    free(strLevel);
}

void GameGUI::showScene(std::vector<RoleInfo> rolesInfo, int selectRole, int step, bool closeSound)
{
    SDL_Rect src, dest;

    if(SDL_MUSTLOCK(this->mainWnd)) {
        SDL_LockSurface(this->mainWnd);
    }

    SDL_SetColorKey(this->selectedB, SDL_SRCCOLORKEY, SDL_MapRGB(this->selectedB->format, 0, 0, 255));
    SDL_SetColorKey(this->selectedT, SDL_SRCCOLORKEY, SDL_MapRGB(this->selectedT->format, 0, 0, 255));
    SDL_SetColorKey(this->closeSoundFlag, SDL_SRCCOLORKEY, SDL_MapRGB(this->selectedT->format, 0, 0, 255));

    /* 画背景 */
    SDL_BlitSurface(this->background, NULL, this->mainWnd, NULL);
    dest.x = this->mainWnd->w / 2 - this->levelTile->w / 2;
    dest.y = 70;
    SDL_BlitSurface(this->levelTile, NULL, this->mainWnd, &dest);
    /* 画小曹同志 */
    src.x = 0;
    src.y = 0;
    src.w = 2 * GameSystem::UNIT_WIDTH;
    src.h = src.w;
    dest.x = GameSystem::FIELD_LEFT + rolesInfo[0].getRoleInfoX() * GameSystem::UNIT_WIDTH;
    dest.y = GameSystem::FIELD_TOP + rolesInfo[0].getRoleInfoY() * GameSystem::UNIT_WIDTH;
    SDL_BlitSurface(this->roles, &src, this->mainWnd, &dest);
    if(selectRole == 0) {
        dest.x = dest.x + src.w / 2 - this->selectedT->w / 2;
        SDL_BlitSurface(this->selectedT, NULL, this->mainWnd, &dest);
        dest.y = dest.y + src.h - this->selectedB->h;
        SDL_BlitSurface(this->selectedB, NULL, this->mainWnd, &dest);
    }
    /* 画五虎上将 */
    for(int i = 1; i < 6; i++){
        if(rolesInfo[i].getRoleInfoK() == GameSystem::ROLE_CARD_X){
            src.x = (((i - 1) / 2) + 1) * GameSystem::UNIT_WIDTH * 2;
            src.y = ((i + 1) % 2) * GameSystem::UNIT_WIDTH;
            src.w = GameSystem::UNIT_WIDTH * 2;
            src.h = GameSystem::UNIT_WIDTH;
        }else{
            src.x = (i + 7) * GameSystem::UNIT_WIDTH;
            src.y = 0;
            src.w = GameSystem::UNIT_WIDTH;
            src.h = src.w * 2;
        }
        dest.x = GameSystem::FIELD_LEFT + rolesInfo[i].getRoleInfoX() * GameSystem::UNIT_WIDTH;
        dest.y = GameSystem::FIELD_TOP + rolesInfo[i].getRoleInfoY() * GameSystem::UNIT_WIDTH;
        SDL_BlitSurface(this->roles, &src, this->mainWnd, &dest);
        if(selectRole == i) {
            dest.x = dest.x + src.w / 2 - this->selectedT->w / 2;
            SDL_BlitSurface(this->selectedT, NULL, this->mainWnd, &dest);
            dest.y = dest.y + src.h - this->selectedB->h;
            SDL_BlitSurface(this->selectedB, NULL, this->mainWnd, &dest);
        }
    }

    /* 画路人甲、乙、丙、丁 */
    src.x = 6 * GameSystem::UNIT_WIDTH;
    src.y = GameSystem::UNIT_WIDTH;
    src.w = GameSystem::UNIT_WIDTH;
    src.h = src.w;
    for( int i = 6; i < 10; i++) {
        dest.x = GameSystem::FIELD_LEFT + rolesInfo[i].getRoleInfoX() * GameSystem::UNIT_WIDTH;
        dest.y = GameSystem::FIELD_TOP + rolesInfo[i].getRoleInfoY() * GameSystem::UNIT_WIDTH;
        SDL_BlitSurface(this->roles, &src, this->mainWnd, &dest);
        if(selectRole == i) {
            dest.x = dest.x + src.w / 2 - this->selectedT->w / 2;
            SDL_BlitSurface(this->selectedT, NULL, this->mainWnd, &dest);
            dest.y = dest.y + src.h - this->selectedB->h;
            SDL_BlitSurface(this->selectedB, NULL, this->mainWnd, &dest);
        }
    }

    /* 写上已走步数 */
    SDL_Color color = {255, 255, 255};
    char *strStep = new char[256];
    memset(strStep, 0, 256);
    sprintf(strStep,"%i",step);
    SDL_Surface *steps = TTF_RenderUTF8_Blended(font, strStep, color);
    dest.x = 250;
    dest.y = 488;
    SDL_BlitSurface(steps, NULL, this->mainWnd, &dest);
    SDL_FreeSurface(steps);
    free(strStep);

    /* 看是否要画上关闭声音 */
    if(closeSound) {
        dest.x = GameSystem::SOUND_L + 5;
        dest.y = GameSystem::RESET_SOUND_TOP + 3;
        SDL_BlitSurface(this->closeSoundFlag, NULL, this->mainWnd, &dest);
    }

    if(SDL_MUSTLOCK(this->mainWnd)) {
        SDL_UnlockSurface(this->mainWnd);
    }

    SDL_Flip(this->mainWnd);
}

void GameGUI::showHelp()
{
    SDL_Rect dest;

    SDL_Surface *help = IMG_Load("image/help.jpg");
    if(help == NULL){
        std::cout<<"Error when try to load help.jpg"<<std::endl;
        return;
    }

    SDL_BlitSurface(this->background, NULL, this->mainWnd, NULL);

    SDL_Color color = {255, 255, 255};
    SDL_Surface *helpTitle = TTF_RenderUTF8_Blended(this->font, "游戏说明", color);
    dest.x = this->mainWnd->w / 2 - helpTitle->w / 2;
    dest.y = 70;
    SDL_BlitSurface(helpTitle, NULL, this->mainWnd, &dest);
    SDL_FreeSurface(helpTitle);

    dest.x = GameSystem::FIELD_LEFT - 23;
    dest.y = GameSystem::FIELD_TOP - 24;
    SDL_BlitSurface(help, NULL, this->mainWnd, &dest);
    SDL_FreeSurface(help);

    SDL_Flip(this->mainWnd);
}

GameGUI::~GameGUI()
{
    SDL_FreeSurface(this->background);
    SDL_FreeSurface(this->roles);
    SDL_FreeSurface(this->levelTile);
    SDL_FreeSurface(this->closeSoundFlag);
    SDL_FreeSurface(this->selectedB);
    SDL_FreeSurface(this->selectedT);
}
